package com.example.governmentcrawler.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @projectName: GovernmentCrawler
 * @package: com.example.governmentcrawler.utils
 * @className: SendHttp
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2024/3/9 12:15
 * @version: 1.0
 **/
@Component
public class SendHttp {
    public JSONObject sendHTTPPost(String url, String jsonString, String header) throws RuntimeException{
        //        System.out.println(procurementForGov);
        //创建缓冲区
//        StringBuffer myString=new StringBuffer();
        StringBuilder myString=new StringBuilder();
        try {
            HttpClient client = new HttpClient();
            // 连接超时
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
            // 读取数据超时
            client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
            client.getParams().setContentCharset("UTF-8");
            PostMethod postMethod = new PostMethod(url);

            postMethod.setRequestHeader("content-type", header);


            StringRequestEntity requestEntity = new StringRequestEntity(jsonString,header, "UTF-8");
            postMethod.setRequestEntity(requestEntity);

            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {

                InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyAsStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    myString.append(line);
                }
                reader.close();
            } else {
                throw new RuntimeException("接口连接失败！,状态码为"+HttpStatus.SC_OK);
            }
        } catch (Exception e) {
            throw new RuntimeException("接口连接失败！");
        }

        String str = myString.toString();

        JSONObject jsonObject = JSON.parseObject(str);
//        System.out.println(jsonObject);
        return jsonObject;
    }

    public JSONObject sendHTTPPostForPublicHz(String url,String jsonString,String header)throws RuntimeException{
        //        System.out.println(procurementForGov);
        //创建缓冲区
//        StringBuffer myString=new StringBuffer();
        StringBuilder myString=new StringBuilder();
        try {
            HttpClient client = new HttpClient();
            // 连接超时
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
            // 读取数据超时
            client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
            client.getParams().setContentCharset("UTF-8");
            PostMethod postMethod = new PostMethod(url);

            postMethod.setRequestHeader("content-type", header);


            StringRequestEntity requestEntity = new StringRequestEntity(jsonString,header, "UTF-8");
            postMethod.setRequestEntity(requestEntity);

            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {

                InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyAsStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    myString.append(line);
                }
                reader.close();
            } else {
                throw new RuntimeException("接口连接失败！,状态码为"+HttpStatus.SC_OK);
            }
        } catch (Exception e) {
            throw new RuntimeException("接口连接失败！");
        }

        String str = myString.toString();
//        System.out.println(s);
        String newStr = str.substring(1, str.length() - 1);
        JSONObject jsonObject = JSON.parseObject(newStr);
//        System.out.println(jsonObject);
        return jsonObject;
    }

    public String sendHTTPPostForOnline(String url,String jsonString,String header)throws RuntimeException{
        //        System.out.println(procurementForGov);
        //创建缓冲区
//        StringBuffer myString=new StringBuffer();
        StringBuilder myString=new StringBuilder();
        try {
            HttpClient client = new HttpClient();
            // 连接超时
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
            // 读取数据超时
            client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
            client.getParams().setContentCharset("UTF-8");
            PostMethod postMethod = new PostMethod(url);

            postMethod.setRequestHeader("content-type", header);
            StringRequestEntity requestEntity = new StringRequestEntity(jsonString,"application/x-www-form-urlencoded; charset=UTF-8","UTF-8");
            postMethod.setRequestEntity(requestEntity);


            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {

                InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyAsStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    myString.append(line);
                }
                reader.close();
            } else {
                throw new RuntimeException("接口连接失败！,状态码为"+HttpStatus.SC_OK);
            }
        } catch (Exception e) {
            throw new RuntimeException("接口连接失败！");
        }
//        System.out.println(myString);
        String s = myString.toString();
        return s;
    }

    public String sendHTTPPostForTwoAfter(String url) throws RuntimeException{
        StringBuilder myString=new StringBuilder();
        try {
            HttpClient client = new HttpClient();
            // 连接超时
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
            // 读取数据超时
            client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
            client.getParams().setContentCharset("UTF-8");
            GetMethod getMethod = new GetMethod(url);
            int status = client.executeMethod(getMethod);
            if (status == HttpStatus.SC_OK) {

                InputStream responseBodyAsStream = getMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyAsStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    myString.append(line);
                }
                reader.close();
            } else {
                throw new RuntimeException("接口连接失败！,状态码为"+HttpStatus.SC_OK);
            }
        } catch (Exception e) {
            throw new RuntimeException("接口连接失败！");
        }

        String s = myString.toString();
//        System.out.println(s);
//        System.out.println(jsonObject);
        return s;
    }
}
